import { MyGameConfig } from "./myGameConfig"
import { getRandom, getHTMLTags, getSomeCSSNames } from "../utils/fns";
import GameConfig from "../GameConfig";
export default class gameManage extends Laya.Script {
    /** @prop {name:levelLab, tips:"关卡", type:Node}*/
    public levelLab: Laya.Node;
    /** @prop {name:timeLab, tips:"时间", type:Node}*/
    public timeLab: Laya.Node;
    /** @prop {name:intervalLab, tips:"间隔", type:Node}*/
    public intervalLab: Laya.Node;
    /** @prop {name:speedLab, tips:"速度", type:Node}*/
    public speedLab: Laya.Node;
    /** @prop {name:countLab, tips:"数量", type:Node}*/
    public countLab: Laya.Node;
    /** @prop {name:scoreLab, tips:"分数", type:Node}*/
    public scoreLab: Laya.Node;
    /** @prop {name:userInput, tips:"用户输入框", type:Node}*/
    public userInput: Laya.Node;
    /** @prop {name:wolfContainer, tips:"狼容器", type:Node}*/
    public wolfContainer: Laya.Node;
    /** @prop {name:wolfPrefab, tips:"狼", type:Prefab}*/
    public wolfPrefab: Laya.Prefab;
    /** @prop {name:frankPrefab, tips:"frank", type:Prefab}*/
    public frankPrefab: Laya.Prefab;
    /** @prop {name:wxyHomePrefab, tips:"万小息家的开始起点", type:Prefab}*/
    public wxyHomePrefab: Laya.Prefab;
    /* 游戏配置 */
    public gameConfig: MyGameConfig = new MyGameConfig();
    /* 游戏状态 */
    public gameState: number = 0; // 0.未开始 1.游戏中 2.游戏结束
    // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0

    constructor() { super(); }
    onAwake(): void {

    }

    onEnable(): void {
        this.gameInit();
        /* 回主菜单 */
        let exitBtn = this.owner.getChildByName("btnExit");
        exitBtn.on(Laya.Event.MOUSE_DOWN, this, () => {
            /* 跳转到开始页面 */
            Laya.Scene.open("menu.scene");
        })
        /* 重来 */
        let replayBtn = this.owner.getChildByName("btnReplay");
        replayBtn.on(Laya.Event.MOUSE_DOWN, this, () => {
            /* 移除所有动画 */
            let i = this.wolfContainer.numChildren;
            for (let z = 0; z < i; z++) {
                Laya.Tween.clearAll(this.wolfContainer.getChildAt(z));
            }
            /* 消灭所有狼 */
            this.wolfContainer.removeChildren();
            /* 重来 */
            this.gameInit();
        })
    }

    onDisable(): void {
    }
    /* 随机取单词 */
    gameRandomGetWord(_difficult = false) {
        return !_difficult ? getHTMLTags()[getRandom(0, getHTMLTags().length - 1)] : getSomeCSSNames()[getRandom(0, getSomeCSSNames().length - 1)]
    }
    /* 时钟事件 */
    gameTimerEvent(): void {
        if (this.gameConfig.time >= 0 || this.gameState == 2) {
            if (this.gameConfig.time % this.gameConfig.interval == 0) {
                this.generateWolf(this.gameCalcCount());
                if (getRandom(0, 50) > 40) {
                    this.generateFrank();
                }
            }
            /* 提示牌展示 */
            this.gameSignShowStatus();
            /* 时间减少 */
            this.gameConfig.time--;
        } else {
            this.gameClearTimer();
            if (this.gameState == 1) {
                this.gameNextLevel();
            }
        }

    }
    /* 清除定时器 */
    gameClearTimer() {
        Laya.timer.clear(this, this.gameTimerEvent);
    }
    /* 游戏去下个关卡 */
    gameNextLevel() {
        this.gameConfig.level++;
        this.gameConfig.time = 30;
        this.gameConfig.interval = this.gameConfig.level > 20 ? 3 : this.gameConfig.level > 10 ? 4 : 5;
        this.gameConfig.speed = this.gameConfig.level;
        this.gameState = 1;
        Laya.timer.loop(1000, this, this.gameTimerEvent);
    }
    /* 提示牌显示当前状态 */
    gameSignShowStatus() {
        let tempArrNames = [{ key: 'time', des: "时间" }, { key: 'level', des: "关卡" }, { key: 'interval', des: "间隔" }, { key: 'speed', des: "速度" }, { key: 'count', des: '数量' }, { key: 'score', des: '分数' }];
        tempArrNames.forEach(item => {
            if (this[item.key + 'Lab']) { this[item.key + 'Lab'].text = item.des + ": " + this.gameConfig[item.key]; }
        })
    }
    /* 生成狼的数量计算 */
    gameCalcCount(): number {
        return this.gameConfig.time > 20 ? 1 : this.gameConfig.time > 10 ? 2 : 3;
    }
    /* 游戏初始化 */
    gameInit(): void {
        /* 初始化参数 */
        this.gameConfig = new MyGameConfig();
        /* 隐藏游戏结束提示框 */
        let gameOverdialog = this.owner.getChildByName('gameOverDialog');
        let gameOverBtn = gameOverdialog.getChildByName('replayBtn');
        (gameOverdialog as Laya.Sprite).visible = false;
        /* 绑定重新开始事件 */
        gameOverBtn.on(Laya.Event.CLICK, this, () => {
            this.gameInit();
        })
        /* 注册按键 */
        this.userInput.on(Laya.Event.KEY_DOWN, this, (e) => {
            if (e.keyCode == "13") {
                /* 用户输入 */
                let userInputText = (this.userInput as Laya.Input).text.toLocaleLowerCase();
                // 按下回车键
                this.killWolf(userInputText);
                this.killFrank(userInputText);
                (this.userInput as Laya.Input).text = "";
            }
        })
        // this.userInput.on(Laya.Event., this, (e) => {
        //     if (e.keyCode == "13") {
        //         // 按下回车键

        //     }
        // })
        /* 生成万小y的家 */
        this.ganerateWxyHome(5);
        /* 生成狼 */
        this.generateWolf(this.gameCalcCount());
        /* 置输入框焦点 */
        (this.userInput as Laya.TextInput).focus = true;
        /* 提示牌展示 */
        this.gameSignShowStatus();
        /* 时钟事件 */
        this.gameState = 1; // 游戏开始
        Laya.timer.loop(1000, this, this.gameTimerEvent)
    }
    /* 消灭狼 */
    killWolf(_wolfName: string = 'test') {
        let tempWolf = this.wolfContainer.getChildByName("wolf-" + _wolfName);
        if (tempWolf) {
            Laya.Tween.clearTween(tempWolf);
            /* 增加分数 */
            this.gameConfig.score++;
            Laya.Tween.to(tempWolf, { rotation: -120, alpha: 0 }, 500, null, new Laya.Handler(this, () => {
                tempWolf.removeSelf();
                /* 减少狼的数量 */
                this.gameConfig.count > 0 ? this.gameConfig.count-- : void (0)
            }));
        }

    }
    /* 消灭Frank */
    killFrank(_wolfName: string = 'test') {
        let tempFrank = this.wolfContainer.getChildByName("frank-" + _wolfName);
        if (tempFrank) {
            Laya.Tween.clearTween(tempFrank);
            /* 增加分数 */
            this.gameConfig.score += 100; // 增加100分


            let tempFrankImage = tempFrank.getChildByName("goldShip");
            Laya.Tween.to(tempFrank, { rotation: -180 }, 500, null, new Laya.Handler(this, () => {
                (tempFrankImage as Laya.Image).skin = "wanxiaoxi/ani/ani-0.png";
                Laya.Tween.to(tempFrank, { scale: 20 }, 100, null, new Laya.Handler(this, () => {
                    (tempFrankImage as Laya.Image).skin = "wanxiaoxi/ani/ani-1.png";
                    Laya.Tween.to(tempFrank, { scale: 30 }, 100, null, new Laya.Handler(this, () => {
                        (tempFrankImage as Laya.Image).skin = "wanxiaoxi/ani/ani-2.png";
                        Laya.Tween.to(tempFrank, { scale: 40 }, 100, null, new Laya.Handler(this, () => {
                            (tempFrankImage as Laya.Image).skin = "wanxiaoxi/ani/ani-3.png";
                            Laya.Tween.to(tempFrank, { scale: 50 }, 100, null, new Laya.Handler(this, () => {
                                (tempFrankImage as Laya.Image).skin = "wanxiaoxi/ani/ani-4.png";
                                Laya.Tween.to(tempFrank, { scale: 100 }, 100, null, new Laya.Handler(this, () => {
                                    (tempFrankImage as Laya.Image).skin = "wanxiaoxi/ani/ani-5.png";
                                    Laya.Tween.to(tempFrank, { scale: 200 }, 100, null, new Laya.Handler(this, () => {
                                        (tempFrankImage as Laya.Image).skin = "wanxiaoxi/ani/ani-6.png";
                                        tempFrank.removeSelf();
                                        /* 移除所有动画 */
                                        let i = this.wolfContainer.numChildren;
                                        for (let z = 0; z < i; z++) {
                                            Laya.Tween.clearAll(this.wolfContainer.getChildAt(z));
                                        }
                                        /* 加分 */
                                        this.gameConfig.score += this.wolfContainer.numChildren;
                                        /* 消灭所有狼 */
                                        this.wolfContainer.removeChildren();
                                        /* 狼的数量为0 */
                                        this.gameConfig.count = 0;
                                    }));


                                }));

                            }));
                        }));

                    }));

                }));

            }));
        }

    }
    /* 生成万小y的家 */
    ganerateWxyHome(_num: number = 1) {
        for (let i: number = 0; i < _num; i++) {
            let tempWxyHome: Laya.Sprite = this.wxyHomePrefab.create(); // 万小y的家
            tempWxyHome.x = this.gameConfig.wxxHomeStartPos.x + (i * (tempWxyHome.width + 5));
            tempWxyHome.y = this.gameConfig.wxxHomeStartPos.y
            tempWxyHome.zOrder = 0;
            tempWxyHome.name = "wxyHome-" + i;
            this.owner.addChild(tempWxyHome)
        }
    }
    /* 生成frank */
    generateFrank() {
        let tempFrank = this.frankPrefab.create();
        let tempWold = this.gameRandomGetWord(true); // 困难模式
        tempFrank.name = "frank-" + tempWold;
        tempFrank.x = this.gameConfig.minPos.x || 0;
        tempFrank.y = getRandom(this.gameConfig.minPos.y, this.gameConfig.maxPos.y - 50);
        let tempLab = tempFrank.getChildByName("frankLab");
        tempLab.text = tempWold;
        this.wolfContainer.addChild(tempFrank);
        Laya.Tween.to(tempFrank, { x: this.gameConfig.maxPos.x - tempFrank.width }, 5000, Laya.Ease.bounceIn, new Laya.Handler(this, () => {
            Laya.Tween.to(tempFrank, { x: this.gameConfig.minPos.x }, 5000, null, new Laya.Handler(this, () => {
                tempFrank.removeSelf();
            }));
        }));
    }
    /* 生产狼 */
    generateWolf(_num: number = 1, _labelText: string | null = null): void {
        for (let i: number = 0; i < _num; i++) {
            let labelText = _labelText ? _labelText : this.gameRandomGetWord();
            let tempWolf = this.wolfPrefab.create(); // 生产狼的实例
            let tempWolfLab = (tempWolf as Laya.Sprite).getChildByName('wolfLab'); // 狼名字标签
            (tempWolfLab as Laya.Label).text = labelText;
            tempWolf.name = `wolf-${labelText}`;
            tempWolf.x = this.gameConfig.minPos.x || 0;
            tempWolf.y = getRandom(this.gameConfig.minPos.y, this.gameConfig.maxPos.y)
            this.wolfContainer.addChild(tempWolf);
            let tempSpeedTime = Math.floor(25000 - (this.gameConfig.speed * 500));
            Laya.Tween.to(tempWolf, { x: this.gameConfig.maxPos.x }, tempSpeedTime > 2000 ? tempSpeedTime : 2000, null, new Laya.Handler(this, () => {
                Laya.Tween.to(tempWolf, { y: this.gameConfig.minPos.y - 10 }, 500, null, new Laya.Handler(this, () => {
                    tempWolf.loadImage("wanxiaoxi/ship.png");
                    tempWolf.name = "wolfArrived" + this.gameConfig.life;
                    this.handlerWolfArrived();
                }));
            }));
            /* 增加狼的数量 */
            this.gameConfig.count++;
        }
    }
    /* 当狼到达的事件 */
    handlerWolfArrived() {
        if (this.gameConfig.life > 0) {
            /* 减少提示板的数量 */
            this.gameConfig.count > 0 ? this.gameConfig.count-- : void (0);
            let tempWxyHome = this.owner.getChildByName("wxyHome-" + (this.gameConfig.life - 1))
            let roleImg = (tempWxyHome as Laya.Sprite).getChildByName("roleImg");
            (roleImg as Laya.Image).skin = "wanxiaoxi/wolf.png";
            this.gameConfig.life--;
        } else {
            this.gameOver(); //游戏结束
        }
    }
    /* 游戏结束 */
    gameOver() {
        this.gameClearTimer();
        this.gameState = 2; //游戏结束
        //Laya.Tween.clearAll(this.wolfContainer);// 清除缓动
        this.wolfContainer.removeChildren();
        let gameOverdialog = this.owner.getChildByName('gameOverDialog');
        let gameOverLab = gameOverdialog.getChildByName('gameOverScoreLab');
        (gameOverLab as Laya.Label).text = "游戏结束,您的得分为: " + this.gameConfig.score;
        (gameOverdialog as Laya.Sprite).visible = true;
    }
}